package com.qf.servlet;

import com.qf.service.UserService;
import com.qf.service.UserServiceImpl;
import com.qf.utlis.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取前端传过来的参数
        String uname=request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        //判断用户名密码是否为空
        if(MyUtil.isEmpty(uname) || MyUtil.isEmpty(password)){
            response.sendRedirect(request.getContextPath()+"/login");
        }
        //走登录逻辑
        UserService userservice=new UserServiceImpl();
        boolean result = userservice.login(uname,password);

        if(result){
            session.setAttribute("uname",uname);
            response.sendRedirect(request.getContextPath()+"/list");
        }else{
            response.sendRedirect(request.getContextPath()+"/login.html");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
